//
//  NSString+Attribute.m
//  Coding
//
//  Created by Zhan Yu on 15/12/29.
//  Copyright © 2015年 Coding. All rights reserved.
//

#import "NSString+Attribute.h"

@implementation NSString (Attribute)

+ (NSAttributedString*)getAttributeFromText:(NSString*)text emphasizeTag:(NSString*)tag emphasizeColor:(UIColor*)color
{
    if (text.length==0)
    {
        return nil;
    }
    
    NSString *sepratorStart=[NSString stringWithFormat:@"<%@>",tag];
    NSString *sepratorEnd=[NSString stringWithFormat:@"</%@>",tag];
    
    NSMutableString *mutTitle=[[NSMutableString alloc] initWithString:text];
    NSMutableAttributedString *resultStr=[NSMutableAttributedString new];
    
    while ([mutTitle rangeOfString:sepratorStart].location!=NSNotFound)
    {
        NSRange startRange=[mutTitle rangeOfString:sepratorStart];
        
        [resultStr appendAttributedString:[[NSMutableAttributedString alloc] initWithString:[mutTitle substringWithRange:NSMakeRange(0,startRange.location)]]];
        [mutTitle deleteCharactersInRange:NSMakeRange(0,startRange.location+startRange.length)];
        
        if ([mutTitle rangeOfString:sepratorEnd].location!=NSNotFound)
        {
            NSRange endRange=[mutTitle rangeOfString:sepratorEnd];
            NSRange storeRange=NSMakeRange(resultStr.length, endRange.location);
            [resultStr appendAttributedString:[[NSMutableAttributedString alloc] initWithString:[mutTitle substringWithRange:NSMakeRange(0,endRange.location)]]];
            resultStr=[NSString getAttributeFromText:resultStr range:storeRange emphasizeColor:color];
            [mutTitle deleteCharactersInRange:NSMakeRange(0,endRange.location+endRange.length)];
        }
    }
    //尾部
    if (mutTitle.length>0)
    {
        [resultStr appendAttributedString:[[NSMutableAttributedString alloc] initWithString:mutTitle]];
    }
    return resultStr;
}


+ (NSAttributedString*)getAttributeFromText:(NSString*)text emphasize:(NSString*)emphasize emphasizeColor:(UIColor*)color
{
    NSMutableAttributedString *titleColorStr=[[NSMutableAttributedString alloc] initWithString:text];
    [titleColorStr addAttribute:NSForegroundColorAttributeName value:color range:[text rangeOfString:emphasize]];
    return [titleColorStr copy];
}


+ (NSMutableAttributedString*)getAttributeFromText:(NSMutableAttributedString*)text range:(NSRange)range emphasizeColor:(UIColor*)color
{
    [text addAttribute:NSForegroundColorAttributeName value:color range:range];
    return text;
}

+ (NSString*)getStr:(NSString*)str removeEmphasize:(NSString*)emphasize
{
    NSString *sepratorStart=[NSString stringWithFormat:@"<%@>",emphasize];
    NSString *sepratorEnd=[NSString stringWithFormat:@"</%@>",emphasize];
    NSString *resultStr=str;
    resultStr=[resultStr stringByReplacingOccurrencesOfString:sepratorStart withString:@""];
    resultStr=[resultStr stringByReplacingOccurrencesOfString:sepratorEnd withString:@""];
    return resultStr;
}
+ (NSAttributedString *)getAttributeFromContentText:(NSString *)text {
    
//    NSString *testStr = [[NSString alloc] initWithUTF8String:[text UTF8String]];
//     NSMutableAttributedString *testStr =[[NSMutableAttributedString alloc] initWithString:text];
//    text = @"&nbsp; 3月31日，日本气象厅宣布东京市中心的樱花进入盛开期。今年的花期较往年早了3天，较去年晚了2天。经气象厅工作人员确认，靖国神社的标本树已有80%以上开花。　　东京上野公园里挤满了赏樱的人们。一对中年夫妇特意从埼玉县过来赏樱，丈夫略显羞涩地说：“听说这里的樱花全开了，我就叫妻子一起来了。这是我们第一次欣赏夜晚的樱花，偶尔两个人一起走走也不错。”在上野公园赏夜樱的人们（3月31日晚，东京台东区，石井理惠摄）一个小女孩伸手去摸樱花（3月31日下午，东京千鸟之渊，石井理惠摄）&nbsp;新闻来源：日经新闻网";
    if ([text containsString:@"font color='red'"]) {
        
        NSString *sepratorStart=[NSString stringWithFormat:@"<%@>",@"font color='red'"];
        //    id myarray= [text componentsSeparatedByString:sepratorStart];
        //    NSArray *arrayTitle = (NSArray *)myarray;
        
        NSArray *arrayTitle =[text componentsSeparatedByString:sepratorStart];
        NSMutableAttributedString *title=[[NSMutableAttributedString alloc] init];
        
        //    if ([text rangeOfString:sepratorStart].location ==NSNotFound) {
        //
        //    };
        
        //    NSArray *arrayTitle =[text componentsSeparatedByString:sepratorStart];
        //    NSMutableAttributedString *title=[[NSMutableAttributedString alloc] init];
        for (NSInteger i=0; i<arrayTitle.count; i++) {
            if (i==0) {
                
                NSString *title0 = arrayTitle[0];
                if (title0.length>45) {
                    NSInteger mylength = title0.length;
                    title0 = [title0 substringFromIndex:mylength-40];
                    title0 = [NSString stringWithFormat:@"...%@",title0];
                    [title appendAttributedString:[[NSAttributedString alloc] initWithString:title0]];
                }
                else
                {
                    [title appendAttributedString:[[NSAttributedString alloc] initWithString:arrayTitle[0]]];
                }
                continue ;
            }
            if (i==arrayTitle.count-1) {
                NSString *str =arrayTitle[i];
                NSRange range =[str rangeOfString:@"</font>"];
                NSMutableAttributedString *attrStr =[[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@%@",[str substringToIndex:range.location],[str substringFromIndex:range.location+7]]];
                [attrStr addAttributes:@{NSForegroundColorAttributeName: [UIColor redColor]} range:NSMakeRange(0, range.location)];
                [title appendAttributedString:attrStr];
                continue ;
            }
            NSString *str =arrayTitle[i];
            NSRange range =[str rangeOfString:@"</font>"];
            NSMutableAttributedString *attrStr =[[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@%@",[str substringToIndex:range.location],[str substringFromIndex:range.location+7]]];
            [attrStr addAttributes:@{NSForegroundColorAttributeName: [UIColor redColor]} range:NSMakeRange(0, range.location)];
            [title appendAttributedString:attrStr];
            continue ;
            
        }
        
        
        return title;
    }
    else
    {
        NSMutableAttributedString *attrStr =[[NSMutableAttributedString alloc] initWithString:text];
        return attrStr;
    }
}

@end
